{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Article_Sentiment.ipynb",
      "version": "0.3.2",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "2mnreShE1u4B",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Get the sentiment of articles from the internet\n",
        "\n",
        "#pip install newspaper3k\n",
        "#pip install textblob\n",
        "#pip install nltk\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fnJ-oin31s6G",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Import the libraries\n",
        "from textblob import TextBlob\n",
        "import nltk\n",
        "from newspaper import Article"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kmBoXpxjr6e4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Get the article\n",
        "url = 'https://everythingcomputerscience.com/'\n",
        "article = Article(url)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "K5Vwx_S0sT8H",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "599d0b0f-96f0-4688-ab24-c2eb25d821b9"
      },
      "source": [
        "# Do some NLP\n",
        "article.download() #Downloads the link’s HTML content\n",
        "article.parse() #Parse the article\n",
        "nltk.download('punkt')#1 time download of the sentence tokenizer\n",
        "article.nlp()#  Keyword extraction wrapper"
      ],
      "execution_count": 134,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[nltk_data] Downloading package punkt to /root/nltk_data...\n",
            "[nltk_data]   Package punkt is already up-to-date!\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nX1NHvTytvV2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 104
        },
        "outputId": "6da71726-418b-4ca2-ed3f-85da99d8bfd4"
      },
      "source": [
        "#Get the summary of the article\n",
        "text = article.summary\n",
        "\n",
        "#print text\n",
        "print(text)\n"
      ],
      "execution_count": 135,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Computer Science (CS)Computer Science (CS) is the study of the principles and use of computers.\n",
            "Simply put it's the science that deals with the theory and methods of processing information in digital computers, the design of computer hardware and software, and the applications of computers.\n",
            "A computer is simply a tool for a computer scientist, like a telescope for an astronomer.\n",
            "Teach yourself !\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "v42ok4ZMr_dO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "80447150-a4a1-44dc-f707-24a3dba77b4b"
      },
      "source": [
        "#Create Text Blob Object\n",
        "#NOTE: You can treat TextBlob objects as if they were Python strings that learned how to do Natural Language Processing.\n",
        "obj = TextBlob(text)\n",
        "\n",
        "#returns the sentiment of text\n",
        "#by returning a value between -1.0 and 1.0\n",
        "sentiment = obj.sentiment.polarity\n",
        "print(sentiment)"
      ],
      "execution_count": 136,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "36D1Aul1s01X",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "3564c9c6-f818-4e73-9e7a-d31cf7039c18"
      },
      "source": [
        "#Print if the text was neutral, positive, or negative\n",
        "if sentiment == 0:\n",
        "  print('The text is neutral')\n",
        "elif sentiment > 0:\n",
        "  print('The text is positive')\n",
        "else:\n",
        "  print('The text is negative')"
      ],
      "execution_count": 137,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "The text is neutral\n"
          ],
          "name": "stdout"
        }
      ]
    }
  ]
}
